﻿jQuery.fn.popupeditor = function (options) {
	var defaults =
	{
		length: -1,
		columns: 90,
		rows: 18,
		width: -1,
		height: -1,
		text: '',
		onSave: function () { },
		autoOpen: false
	};

	var $ = jQuery;
	var settings = $.extend({}, defaults, options);
	var target = $(this);

	if (settings.height == -1) {
		if (settings.width != -1) {
			alert("popupeditor: height & width must be both specified or both unspecified (-1)");
			return;
		}

		if (settings.rows == -1) {
			alert("popupeditor: either height/width or rows/columns must be set");
			return;
		}
		if (settings.columns == -1) {
			alert("popupeditor: rows & columns must be both specified or both unspecified (-1)");
			return;
		}
		settings.height = settings.rows * 15;
		settings.width = settings.columns * 9;
	}
	else {
		if (settings.width == -1) {
			alert("popupeditor: height & width must be both specified or both unspecified (-1)");
			return;
		}
		if (settings.rows != -1) {
			alert("popupeditor: either height/width or rows/columns must be set. -- Not both");
			return;
		}
		if (settings.columns != -1) {
			alert("popupeditor: rows & columns must be both specified or both unspecified (-1)");
			return;
		}
		settings.rows = settings.height / 15;
		settings.columns = settings.width / 9;
	}

	$("#pupedit-Tabs").tabs();
	$('#pupedit-text').text(target.val());
	if (settings.length == -1)
		$('#pupedit-max').hide();
	else
		$('#pupedit-text').maxlength(settings);

	$('#pupedit-Save').enable().click(function () {
		var txt = $('#pupedit-text').text();
		$(target).val(txt);

		settings.onSave();
		CloseDialog();
	});
	$('#pupedit-Cancel').enable().click(CloseDialog);

	$('#pupedit-text')
		.attr('cols', settings.columns)
		.attr('rows', settings.rows)
//        .text(settings.text)
		.css({ 'width': settings.width, 'height': settings.height });

	settings.height += 140;
	settings.width += 42;
	$('#pupedit-Tabs').css({ 'width': settings.width, 'height': settings.height });

	settings.height = 'auto';
	settings.width = 'auto';
	$('#pupedit-dialog').dialog(settings)
		.bind("dialogopen", function (event, ui) {
			// #pupedit-Tabs == #pupedit-dialog ==  (.ui-dialog - .ui-dialog-titlebar ) == tabs-edit + ui-tabs-nav
			// tabs-edit == #pupedit-text.outerHeight() + #wmd-button-bar.outerHeight() + pupedit-Save.outerHeight()

			var height = $('#pupedit-text').outerHeight() + $('#wmd-button-bar').outerHeight() + $('#pupedit-Save').outerHeight();
			var width = $('#pupedit-text').outerWidth() + 30;
			$('#wmd-preview').css({ 'width': width, 'height': height });

			width = 'auto';
			$('#tabs-edit').css({ 'width': width, 'height': height });
			$('#tabs-preview').css({ 'width': width, 'height': height });
			$('#tabs-help').css({ 'width': width, 'height': height });

		})
		.attr('title', 'Description for :' + settings.title);

	$('#pupedit-dialog').dialog("open");
	$('#pupedit-text').setFocus();

	function CloseDialog() {
		$('#pupedit-dialog').dialog("close");
		$("#pupedit-Save").unbind("click");
		$("#pupedit-cancel").unbind("click");
	}
}